
package com.jf.cloud.rbac.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.auth.feign.TokenFeignClient;
import com.jf.cloud.api.rbac.dto.UserRoleDTO;
import com.jf.cloud.api.rbac.feign.UserRoleFeignClient;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.rbac.mapper.UserRoleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;

import java.util.List;

/**
 * @author zz
 * @date 2020/7/15
 */
@RestController
@Hidden
public class UserRoleFeignController implements UserRoleFeignClient {

    private static final Logger logger = LoggerFactory.getLogger(UserRoleFeignController.class);

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private TokenFeignClient tokenFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = CacheNames.MENU_ID_LIST_KEY, key = "#userRoleDTO.userId")
    public ServerResponseEntity<Void> saveByUserIdAndSysType(UserRoleDTO userRoleDTO) {

        if(CollUtil.isEmpty(userRoleDTO.getRoleIds())){
            return ServerResponseEntity.success();
        }
        //保存用户与角色关系
        userRoleMapper.insertUserAndUserRole(userRoleDTO.getUserId(), userRoleDTO.getRoleIds());
        return ServerResponseEntity.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = CacheNames.MENU_ID_LIST_KEY, key = "#userRoleDTO.userId")
    public ServerResponseEntity<Void> updateByUserIdAndSysType(UserRoleDTO userRoleDTO) {
        // 不管权限具体有没有发生变动，都直接删除token缓存
        tokenFeignClient.deleteToken(userRoleDTO.getUserId(), userRoleDTO.getSysType());
        //先删除用户与角色关系
        userRoleMapper.deleteByUserId(userRoleDTO.getUserId());

        if(CollUtil.isEmpty(userRoleDTO.getRoleIds())){
            return ServerResponseEntity.success();
        }
        //保存用户与角色关系
        userRoleMapper.insertUserAndUserRole(userRoleDTO.getUserId(), userRoleDTO.getRoleIds());
        return ServerResponseEntity.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = CacheNames.MENU_ID_LIST_KEY, key = "#userId")
    public ServerResponseEntity<Void> deleteByUserIdAndSysType(Long userId) {
        userRoleMapper.deleteByUserId(userId);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<List<Long>> getRoleIds(Long userId) {
        return ServerResponseEntity.success(userRoleMapper.getRoleIds(userId));
    }
}
